package com.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.adpter.FriendAdapter;
import com.android.ocean.util.common.StringUtil;
import com.android.ocean.util.common.TranHelper;
import com.android.ocean.util.upgrade.DialogHelper;
import com.contact.ChatActivity;
import com.db.DBDao;
import com.http.FriendDelTask;
import com.http.FriendListTask;
import com.matang.R;
import com.modle.CFriendContent;
import com.modle.IFriend;
import com.modle.User;
import com.util.AppUtil;

/**
 * 
 * @ClassName:XFriendListView
 * @Description:TODO
 * @author zhuw
 * @date 2013-8-4 下午10:45:02
 */
public class XFriendListView extends BaseFrameLayout implements
		FriendListTask.CallBack, FriendDelTask.CallBack {
	String TAG = "XFriendListView";
	ArrayList<IFriend> dataList = new ArrayList<IFriend>();
	FriendAdapter xAdapter;
	ListView xListView;
	String uID;// 用户Id

	public XFriendListView(Context context) {
		super(context);
		init(context);
	}

	public XFriendListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public XFriendListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		this.context = context;
		View view = LayoutInflater.from(context).inflate(R.layout.l_list_txl,
				this);
		xListView = (ListView) view.findViewById(R.id.con_listview);
		loadingView = (View) view.findViewById(R.id.loading_view);

	}

	public void initData(String uID) {
		this.uID = uID;
		loadConList();
	}

	/**
	 * 加载数据
	 */
	private void loadConList() {
		new FriendListTask(this).execute(uID);
	}

	private void fillPane() {

		if (StringUtil.isListNull(dataList)) {
			AppUtil.tsMsg(context, "无数据...");
			xAdapter = new FriendAdapter(new ArrayList<IFriend>(), context);
			   xListView.setAdapter(xAdapter);
		} else {

			xAdapter = new FriendAdapter(dataList, context);
			xListView.setAdapter(xAdapter);
			xListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> ad, View arg1, int pos,
						long arg3) {
					Log.d(TAG, "pos-----:" + dataList.get(pos).isClickable());
					if (dataList.get(pos).isClickable()) {
						CFriendContent data = (CFriendContent) ad.getAdapter()
								.getItem(pos);
						Bundle b = new Bundle();
						b.putSerializable("user", data.user);
						TranHelper.toAct(context, ChatActivity.class, b);
					}
				}
			});
			xListView.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> adapterView,
						View view, int pos, long arg3) {
					if (dataList.get(pos).isClickable()) {
						final int delPos = pos;
						DialogHelper.Confirm(context, "确定删除好友?", null, "删除",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										delFriend(delPos);

									}
								}, "取消", null);
					}
					return false;
				}
			});

		}
	}

	private void delFriend(int pos) {
		showProgressDialog();
		final CFriendContent data = (CFriendContent) dataList.get(pos);
		// 删除网上数据
		new FriendDelTask(this).execute(uID, data.user.id);
		// 清空当前页面数据
		dataList.remove(pos);
		delDataInDB(uID, data.user.id);

	}

	/**
	 * 
	 * @Description:TODO删除最近聊天
	 * @param@param uID
	 * @param@param fID
	 * @return void
	 * @author zhuw
	 * @date 2013-8-4 下午11:28:15
	 */
	private void delDataInDB(String uID, String fID) {
		DBDao m = new DBDao(context);
		m.open();
		m.delRecentUser(uID, fID);
		m.close();
	}

	@Override
	public void procError(String msg) {
		disLoading(loadingView);
		dismissProgressDialog();
		AppUtil.tsMsg(context, msg);

	}

	@Override
	public void procFriendsListData(ArrayList<User> dataList) {
		disLoading(loadingView);
		//this.dataList = dataList;
		fillPane();
	}

	@Override
	public void procDelFreshFriendsListData(ArrayList<IFriend> dataList) {
		dismissProgressDialog();
		fillPane();

	}

}
